<?php

namespace app\controller\wechat;

use app\common\controller\WechatController;
use EasyWeChat\Kernel\Messages\Message;
use hiduoke\services\facade\WechatService;

class Api extends WechatController
{
    public function index()
    {
        $app = WechatService::getApp();
        $app->server->push(function ($message) {
            try {
                $res = event('wechat_message', $message);
                $message = [];
                foreach ($res as $key => $value) {
                    if ($value instanceof Message) {
                        $message[] = $value;
                    }
                }
                return $message[0] ?? '';
            } catch (\Exception $e) {
                return $e->getMessage();
            }
        });
        $response = $app->server->serve();
        $response->send();
    }
}
